<?php

namespace App\Http\Controllers\Logic;

use App\Http\Controllers\Controller;
use App\Model\OAPosition;
use App\Model\OAPositionAuth;
use App\Model\OAPositionMapping;
use Illuminate\Http\Request;

class PositionLogic extends Controller
{
    protected $status_list = ['禁用','正常'];
    //获取职位列表
    public function getPositionList($params)
    {
        $query = OAPosition::query();

        if($title = $params['title']) $query->where('title','like','%' . $title . '%');

        if($status = $params['status']) $query->where('status',$status);

        $list = $query->paginate($params['pagesize'],['title','id','status','authid'],'page')->toArray();

        if($list['data']){
            foreach ($list['data'] as &$val){
                    $val['status'] = $this->status_list[$val['status']];
                    $auth_ids = explode(',',$val['authid']);
                    $auth = OAPositionAuth::query()->whereIn('id',$auth_ids)->pluck('title')->toArray();
                    $val['auth'] = implode(',',$auth);
                    unset($val['authid']);
            }
        }

        $list['status_list'] = $this->status_list;
        return $list;
    }


    public function getMappingList($params)
    {
        $query = new OAPositionMapping();
        $positionList = OAPosition::query()->where('status',1)->pluck('title','id')->toArray();

        $list = $query->paginate($params['pagesize'],['*'],'page')->toArray();
        $list['position_list'] = $positionList;
        if(empty($list['data'])) return $list;
        foreach ($list['data'] as &$item){
            $item['roleid'] = $positionList[$item['roleid']] ?? '';
            $item['mapid'] = $positionList[$item['mapid']] ?? '';
//            $item['roleid'] = (int)[$item['roleid']];
//            $item['mapid'] = (int)[$item['mapid']];
        }
        return $list;
    }
}
